package com.sdlogan.pbudget;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Activity that will display the bar graphs and pie charts for the user
 * @author Sean
 *
 */
public class Graphs extends Activity {
	
	List<dbPackage> inputs;
	List<dbPackage> all;
	Intent myIntent;
	View chartView;
	MoneyTable mt;
	String moneytype;
	
	/**
	 * Create the activity
	 */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphs);
        Bundle bun = new Bundle();
        bun = getIntent().getExtras();
        moneytype = bun.getString("type");
        final LinearLayout chart = (LinearLayout) findViewById(R.id.chart);
        Button pie = (Button) findViewById(R.id.pieselect);
        Button bar = (Button) findViewById(R.id.barselect);
        final Spinner type = (Spinner) findViewById(R.id.chartspin);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
                this, R.array.charts, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        type.setAdapter(adapter1);
        mt = new MoneyTable(this);
        mt.open();
        Calendar cal = Calendar.getInstance();
		String year = Integer.toString(cal.get(Calendar.YEAR),10);
		Date date = new Date();
		String month = new SimpleDateFormat("MMMM").format(date);
        if (type.getSelectedItem().toString().equals("Yearly")) {
        	inputs = mt.getYearly(year);
        }
        else {
        	inputs = mt.getMonthly(month);
        }
        mt.close();
        pie.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				String timeFrame = type.getSelectedItem().toString();
				PieChart pc = new PieChart(timeFrame,inputs,moneytype);
				chartView = pc.getView(getBaseContext());
				chart.removeAllViews();
				chart.addView(chartView);
			}
		});
        bar.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				String timeFrame = type.getSelectedItem().toString();
				BarGraph bg = new BarGraph(timeFrame,inputs);
				chartView = bg.getView(getBaseContext());
				chart.removeAllViews();
				chart.addView(chartView);
			}
		});
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.home:
			Log.i("Sean","Pressed Home");
			myIntent = new Intent(Graphs.this, Main.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.graphs:
			return true;
		case R.id.incomechart:
			Log.i("Sean","income Graph");
			myIntent = new Intent(Graphs.this,Graphs.class);
			myIntent.putExtra("type","Income");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expensechart:
			Log.i("Sean","expense graph");
			myIntent = new Intent(Graphs.this,Graphs.class);
			myIntent.putExtra("type","Expense");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.incomes:
			Log.i("Sean","Pressed Result");
			myIntent = new Intent(Graphs.this, Incomes.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expenses:
			Log.i("Sean","Pressed Expense");
			myIntent = new Intent(Graphs.this,Expenses.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.export:
			Log.i("Sean","Pressed Export");
			myIntent = new Intent(Graphs.this, Export.class);
			startActivityForResult(myIntent,0);
			return true;
		case R.id.newvalue:
			Log.i("Sean","Pressed New");
			return true;
		case R.id.incomenew:
			Log.i("Sean","Pressed Income");
			myIntent = new Intent(Graphs.this,SubmitForm.class);
			myIntent.putExtra("type","Income");
			startActivityForResult(myIntent,0);
			return true;
		case R.id.expensenew:
			Log.i("Sean","Pressed Expense");
			myIntent = new Intent(Graphs.this,SubmitForm.class);
			myIntent.putExtra("type","Expense");
			startActivityForResult(myIntent,0);
			return true;
		default: 
			return false;
		}
		
	}
}
